//
//  PhotoTableViewController.m
//  DingDing
//
//  Created by LHL on 14/12/5.
//  Copyright (c) 2014年 com.gordon. All rights reserved.
//

#import "PhotoTableViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "UserModel.h"
#import "photoMainCell.h"
#import "PhotoCollection_Board.h"
@interface PhotoTableViewController ()

@end

@implementation PhotoTableViewController{

}

-(void)load{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationBarShown = YES;
    self.navigationItem.hidesBackButton = YES;
    self.navigationBarRight = @"取消";
    self.title = @"相册";
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    _group = [[NSMutableArray alloc]init];
    [self getImage];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)getImage{
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc]init];
        [library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
            [group setAssetsFilter:[ALAssetsFilter allPhotos]];
            if (group.numberOfAssets > 0) {
                [_group addObject:group];
                [self.tableView reloadData];
            }
        }failureBlock:^(NSError *error) {
            NSLog(@"%@--%@",error,[error description]);
        }];
        
//    });
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _group.count;
}

ON_SIGNAL3( BeeUINavigationBar, RIGHT_TOUCHED, signal )
{
//    [UIView beginAnimations:nil context:nil];
//    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
//    [UIView setAnimationDuration:0.25];
//    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO];
//    [UIView commitAnimations];
//    [UIView beginAnimations:nil context:nil];
//    [UIView setAnimationDelay:0.15];
//    [self.navigationController popViewControllerAnimated:NO];
//    [UIView commitAnimations];
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    photoMainCell *cell = [tableView dequeueReusableCellWithIdentifier:@"photo"];
    ALAssetsGroup *assetsGroup = [_group objectAtIndex:indexPath.row];
    cell.faceImage.image = [UIImage imageWithCGImage:[assetsGroup posterImage]];
    cell.title.text = [NSString stringWithFormat:@"%@(%d)", [assetsGroup valueForProperty:ALAssetsGroupPropertyName],assetsGroup.numberOfAssets];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"click");
    UIStoryboard *st = [UIStoryboard storyboardWithName:@"PersonalHome" bundle:nil];
    PhotoCollection_Board *vc = [st instantiateViewControllerWithIdentifier:@"collectionVC"];
    vc.model = _group[indexPath.row];
    [vc getpopBlock:^{
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }];
    [self.navigationController pushViewController:vc animated:YES];
}



@end
